document.write("<script language=javascript src='https://jssip.oss-ap-southeast-5.aliyuncs.com/web/jssip-3.3.5.min.js'></script>");

document.write("<script language=javascript src='https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js'></script>");
window.STI = {

  // 登录账号-请在主页面框架调用
  login : function (account, password, channelKey, voipAccount, voipPassword) {
    STI_JSSIP.jssip_login(voipAccount, voipPassword);
  },

  // 适应原先配置
  init : function (clickViewId, divid, account, password, channelKey, voipAccount, voipPassword, senderId, phone, extendId, postUrl) {
    this.call(clickViewId, divid, account, password, channelKey, voipAccount, voipPassword, phone, extendId, postUrl);
  },

  // 呼叫操作
  call : function (clickViewId, divid, account, password, channelKey, voipAccount, voipPassword, phone, extendId, postUrl) {
    console.log('bind call click event', clickViewId, divid);

    // 防止同一id的控件重复绑定点击事件
    $('#' + clickViewId).off('click').on('click', function () {
      // 转换成标准号码格式
      var msisdn = STI_GUI.msisdn_format(phone);

      console.log('call click event', clickViewId, divid, account, voipAccount, voipPassword, msisdn);

      // 是否已点击标志，防止多次被点击
      if (!window.sti_call_click_tag || window.sti_call_click_tag == 0) {
        window.sti_call_click_tag = 1;
        window.sti_call_click_view_id = clickViewId;

        // 控制点击间隔，有些开发把事件绑定了多个事件，导致每次点击重复点击多次
        // 使按钮不能被点击
        $('#' + clickViewId).attr('disabled', 'disabled');

        // 保存到全局
        window.sti_account = account;
        window.sti_password = password;
        window.sti_channel_key = channelKey;
        window.sti_voip_account = voipAccount;
        window.sti_voip_password = voipPassword;
        window.sti_msisdn = msisdn;
        window.sti_extend_id = extendId;
        window.sti_post_url = postUrl;

        STI_GUI.play_sound('pound');

        // 显示呼叫弹框
        // 获取点击坐标位置并设置iframe的显示位置
        var top = $(this).offset().top;
        var left = $(this).offset().left;
        var width = $(this).width();
        STI_GUI.show_call_dialog(divid, top, left, width, msisdn);

        // 登录jssip
        STI_JSSIP.jssip_login(voipAccount, voipPassword);

        // 已连接状态直接呼叫
        if (window.userAgent.isConnected() && window.userAgent.isRegistered()) {
          STI_JSSIP.call(msisdn);
        }
        // 定时获取SIP登录状态
        else {
          // 定时更新登录状态
          var callTimer = setInterval(function () {
            if (window.userAgent.isConnected() && window.userAgent.isRegistered()) {
              clearInterval(callTimer);

              console.info("开始呼叫..." + window.userAgent.isConnected() + " " + window.userAgent.isRegistered());

              STI_JSSIP.call(msisdn);
            }
          }, 300);
          window.sti_call_timer = callTimer;
        }

        var callTextStatusTimer = setInterval(function () {
          var callStatus = $('#sti-call-status-txt').text();
          if (callStatus == 'Calling') {
            STI_GUI.set_call_status_txt('Calling..');
          } else if (callStatus == 'Calling..') {
            STI_GUI.set_call_status_txt('Calling....');
          } else if (callStatus == 'Calling....') {
            STI_GUI.set_call_status_txt('Calling......');
          } else if (callStatus == 'Calling......') {
            STI_GUI.set_call_status_txt('Calling........');
          } else if (callStatus == 'Calling........') {
            STI_GUI.set_call_status_txt('Calling..');
          } else {
            clearInterval(callTextStatusTimer);
          }
        }, 1000);
        window.sti_call_text_status_timer = callTextStatusTimer;

        // 页面关闭
        window.onunload = function() {
          console.log('呼叫页面关闭');
          STI_GUI.call_terminated();
        }
      }
      // 如果有呼叫正在呼叫，弹出确认挂断之前的弹框
      else if (window.sti_call_click_tag = 1) {
        console.log('点击标志：' + window.sti_call_click_tag);

        window.sti_call_new_click_view_id = clickViewId;

        // 显示呼叫弹框
        // 获取点击坐标位置并设置iframe的显示位置
        var top = $(this).offset().top;
        var left = $(this).offset().left;
        var width = $(this).width();

        STI_GUI.show_close_calling_dialog(divid, top, left, width);
      }
    });
  },

}
